package tech.cn.note.excel.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.io.FileUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import tech.cn.note.excel.JXLSTemplateDataFillService;
import tech.cn.note.excel.test.dto.CustomPositionReportDTO;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/report")
@Slf4j
@Validated
public class ExcelController {

    private final JXLSTemplateDataFillService jxlsTemplateDataFillService;

    @GetMapping("excel")
    public void excel(HttpServletResponse response) throws Exception {
        InputStream stream = Files.newInputStream(Paths.get("./script/file/excel/excel.xlsx"));

        Map<String, Object> map = new HashMap<>();
        map.put("list", getData());

        map.put("reportName", "长期价值一号私募证券投资基金");
        map.put("date", LocalDate.now());
        byte[] bytes = jxlsTemplateDataFillService.processToByte(stream, map);
        FileUtil.writeBytes(bytes, "C:\\Users\\summit\\Desktop\\excel.xlsx");;
    }

    public List<CustomPositionReportDTO> getData() {
        List<CustomPositionReportDTO> list = new ArrayList<>();
        CustomPositionReportDTO dto1 = new CustomPositionReportDTO();
        dto1.setTradeId("2024-013_000001");
        dto1.setEffectiveNotional(new BigDecimal("1000000"));

        CustomPositionReportDTO dto2 = new CustomPositionReportDTO();
        dto2.setTradeId("2024-013_000002");
        dto2.setEffectiveNotional(new BigDecimal("88888"));
        list.add(dto1);
        list.add(dto2);
        list.add(dto2);
        return list;
    }
}
